﻿using System.Web.Mvc;
using FoosballEye.Model;
using FoosballEye.Repository;
using Microsoft.Practices.Unity;
using FoosballEye.BLL;

namespace FoosballEye.Web.Controllers
{
    using FoosballEye.Model.Adapters;

    public class JSONDataController: Controller
    {
        //
        // GET: /JSONData/

        private IPlayerRepository _playerRepository;
        //private IGameRepository _gameRepository;
        
        public JSONDataController() : this(null, null)
        {
        }

        /// <summary>
        /// During testing you can pass diffrent repositories to controller
        /// </summary>
        /// <param name="playerRepository"></param>
        public JSONDataController(IPlayerRepository playerRepository, IGameRepository gameRepository)
        {
            //_playerRepository = playerRepository ?? new PlayerRepository();
            _playerRepository = playerRepository ?? ModelContainer.Instance.Resolve<IPlayerRepository>();
            //_gameRepository = gameRepository ?? ModelContainer.Instance.Resolve<IGameRepository>();
        }
       
        //public JsonResult GetPlayer(string login)
        //{
        //    Player player = IsLoginValid(login) ? _playerRepository.GetPlayer(login) : null;     
        //    return Json(player, JsonRequestBehavior.AllowGet); //By default Get is not allowded in JavaScript request
        //}

        public JsonResult GetPlayerFull(string login)
        {
            PlayerFull player = IsLoginValid(login) ? _playerRepository.GetPlayerFull(login) : null;
            return Json(player, JsonRequestBehavior.AllowGet); //By default Get is not allowded in JavaScript request
        }

        public JsonResult GetNewGame(string login)
        {
            NewGame game = IsLoginValid(login) ? GameLogic.GetNewGame() : null;
            return Json(game, JsonRequestBehavior.AllowGet); 
        }

        public JsonResult GetBestPlayers(string login)
        {
            BestPlayers players = IsLoginValid(login) ? GameLogic.GetBestPlayers() : null;
            return Json(players, JsonRequestBehavior.AllowGet); 
        }

        public JsonResult GetStatistics(string login)
        {
            BestPlayers players = IsLoginValid(login) ? GameLogic.GetStatistics() : null;
            return Json(players, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetLastGame(string login)
        {
            LastGame game = IsLoginValid(login) ? GameLogic.GetLastGame() : null;
            return Json(game, JsonRequestBehavior.AllowGet); 
        }

        public JsonResult GetLastGameOfPalyer(string login)
        {
            return null;
        }

        public JsonResult SaveNewGame(string grayDefence, string  grayAttack, string blackDefence, string blackAttack, string grayPoints, string balckPoints, string login, string level)
        {
            try
            {
                Game game = IsLoginValid(login)
                ? GameLogic.SaveNewGame(grayDefence, grayAttack, blackDefence, blackAttack, grayPoints, balckPoints, login, level)
                : null;

                return Json(game, JsonRequestBehavior.AllowGet); 
            }
            catch (BusinessException be)
            {
                return Json(be.Message, JsonRequestBehavior.AllowGet); 
            }
        }

        private bool IsLoginValid(string login)
        {
            return User.Identity.Name.Replace("\\", "") == login;
        }
    }
}